package com.bw.controller;

import com.alibaba.fastjson.JSONObject;
import com.bw.pojo.Role;
import com.bw.service.impl.RoleServiceImpl;
import com.bw.util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/role/*")
public class RoleController extends BaseController{
    RoleServiceImpl service = new RoleServiceImpl();
    //查询
    protected void getRoleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //传递参数
        String reqInfos = req.getParameter("reqInfos");
        //传字符串
        Role role = JSONObject.parseObject(reqInfos, Role.class);
        //Service
        List<Role> list = service.getRoleList(role);
        //给到客户
        resp.getWriter().println(JSONObject.toJSONString(list));
    }

    //添加
    protected void addGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //传递参数
        String reqInfos = req.getParameter("reqInfos");
        //传递字符串
        Role role = JSONObject.parseObject(reqInfos, Role.class);
        //Service
        service.addGet(role);
        //给到客户
        resp.getWriter().println(JSONObject.toJSONString(200));
    }
    //删除
    protected void getToDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //传递参数
        String reqInfos = req.getParameter("reqInfos");
        //传递字符串
        Role role = JSONObject.parseObject(reqInfos, Role.class);
        //service
        service.getToDel(role.getUids());
        //给到客户
        resp.getWriter().println(JSONObject.toJSONString(200));
    }

    //回显
    protected void findByIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //传递参数
        String reqInfos = req.getParameter("reqInfos");
        //传递字符串
        Role role = JSONObject.parseObject(reqInfos, Role.class);
        //service
        Role r =  service.findByIn(role.getRid());
        //给到客户
        resp.getWriter().println(JSONObject.toJSONString(r));
    }

    //修改
    protected void getUpd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //传递参数
        String reqInfos = req.getParameter("reqInfos");
        //传递字符串
        Role role = JSONObject.parseObject(reqInfos, Role.class);
        //service
        service.getUpd(role);
        //给到客户
        resp.getWriter().println(JSONObject.toJSONString(200));
    }
}
